package cn.change360.newappchildrenenglish;

import android.app.Dialog;
import android.content.res.AssetFileDescriptor;
import android.content.res.AssetManager;
import android.gesture.Gesture;
import android.gesture.GestureOverlayView;
import android.graphics.drawable.BitmapDrawable;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.SoundPool;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.activeandroid.query.Select;

import java.io.IOException;

import cn.change360.newappchildrenenglish.mode.Words;
import cn.change360.newappchildrenenglish.utils.FileUtil;

public class WordsWriteActivity extends AppCompatActivity implements View.OnClickListener, GestureOverlayView.OnGesturePerformedListener, GestureOverlayView.OnGesturingListener {

    ImageButton topbar_back, btn_reading,btn_submit;
    TextView topbar_title, word_text, word_times;
    GestureOverlayView mDrawGestureView;

    ImageView word_img;

    SoundPool soundPool;

    Words thisWord;

    Dialog mAlertDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_words_write);

        String word = getIntent().getStringExtra("word");
        thisWord = new Select().from(Words.class).where("word_title=?", word).executeSingle();

        if (thisWord == null) {
            Toast.makeText(this, "参数错误", Toast.LENGTH_SHORT).show();
            finish();
            return;
        }

        soundPool = new SoundPool(10, AudioManager.STREAM_SYSTEM, 5);
        soundPool.load(this, R.raw.s_true, 1);
        soundPool.load(this, R.raw.s_false, 2);

        initView();
    }

    private void initView() {
        topbar_back = (ImageButton) findViewById(R.id.topbar_back);
        topbar_title = (TextView) findViewById(R.id.topbar_title);
        topbar_title.setText("单词练习书写“" + thisWord.getWord_title() + "”");
        topbar_back.setOnClickListener(this);

        btn_reading = (ImageButton) findViewById(R.id.btn_reading);
        btn_submit = (ImageButton) findViewById(R.id.btn_submit);
        btn_reading.setOnClickListener(this);

        mDrawGestureView = (GestureOverlayView) findViewById(R.id.gesture);
        word_img = (ImageView) findViewById(R.id.word_img);
        word_text = (TextView) findViewById(R.id.word_text);
        word_times = (TextView) findViewById(R.id.word_times);

        btn_reading.setOnClickListener(this);
        btn_submit.setOnClickListener(this);

        //设置手势可多笔画绘制，默认情况为单笔画绘制
        mDrawGestureView.setGestureStrokeType(GestureOverlayView.GESTURE_STROKE_TYPE_MULTIPLE);
        //设置手势的颜色(蓝色)
        mDrawGestureView.setGestureColor(getResources().getColor(R.color.gestureColor));
        //设置还没未能形成手势绘制是的颜色(红色)
        mDrawGestureView.setUncertainGestureColor(getResources().getColor(R.color.ungestureColor));
        //设置手势的粗细
        mDrawGestureView.setGestureStrokeWidth(20);
        //停顿时间
        mDrawGestureView.setFadeOffset(20000);
        mDrawGestureView.setFadeEnabled(false);
        //绑定监听器
        mDrawGestureView.addOnGesturePerformedListener(this);
        mDrawGestureView.addOnGesturingListener(this);

        word_text.setText(thisWord.getWord_title());
        word_img.setImageDrawable(new BitmapDrawable(FileUtil.getImageFromAssetsFile(this, "word/" + thisWord.getWord_title() + ".jpg")));

        if (thisWord.getWord_writetimes() == null) {
            word_times.setText("这个单词还没有写过~");
        } else {
            word_times.setText("当前单词已经练习了" + thisWord.getWord_writetimes() + "次");
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.topbar_back:
                finish();
                break;
            case R.id.btn_reading:
                reading();
                break;
            case R.id.btn_submit:
                //停顿时间
                mDrawGestureView.setFadeOffset(0);
                //绑定监听器
                break;
        }
    }

    private void reading() {
        AssetManager am = getAssets();
        AssetFileDescriptor afd;
        try {
            afd = am.openFd("raw/" + thisWord.getWord_title() + ".mp3");
            MediaPlayer mediaPlayer = new MediaPlayer();
            mediaPlayer.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
            mediaPlayer.prepare();
            mediaPlayer.start();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onGesturePerformed(GestureOverlayView gestureOverlayView, Gesture gesture) {

        if (thisWord.getWord_writetimes() == null) {
            thisWord.setWord_writetimes(1l);
        } else {
            thisWord.setWord_writetimes(thisWord.getWord_writetimes() + 1);
        }
        thisWord.save();
        Long newTimes = thisWord.getWord_writetimes();
        View dialogView = LayoutInflater.from(this).inflate(R.layout.dialog_show_score, null);
        TextView score_msg = (TextView) dialogView.findViewById(R.id.score_msg);
        ImageView img_star1 = (ImageView) dialogView.findViewById(R.id.img_star1);
        ImageView img_star2 = (ImageView) dialogView.findViewById(R.id.img_star2);
        ImageView img_star3 = (ImageView) dialogView.findViewById(R.id.img_star3);
        img_star1.setVisibility(View.VISIBLE);
        img_star2.setVisibility(View.VISIBLE);
        img_star3.setVisibility(View.VISIBLE);
        score_msg.setText("熟能生巧，\n已经写了" + newTimes + "次~！");
        word_times.setText("当前单词已经练习了" + newTimes + "次");
        playSound(1);
        Animation starAnimation = AnimationUtils.loadAnimation(this, R.anim.zoom_in);
        img_star1.startAnimation(starAnimation);
        img_star2.startAnimation(starAnimation);
        img_star3.startAnimation(starAnimation);
        dialogView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mAlertDialog.cancel();
            }
        });
        mAlertDialog = new Dialog(this, R.style.theme_dialog);
        mAlertDialog.setContentView(dialogView);
        mAlertDialog.show();

    }
    @Override
    public void onGesturingStarted(GestureOverlayView gestureOverlayView) {
        word_times.setText("正在书写~");
    }

    @Override
    public void onGesturingEnded(GestureOverlayView gestureOverlayView) {
        word_times.setText("请稍等~");
    }

    private void playSound(int m) {
        soundPool.play(m, 1, 1, 0, 0, 1);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //移除绑定的监听器
        mDrawGestureView.removeOnGesturePerformedListener(this);
        mDrawGestureView.removeOnGesturingListener(this);
    }
}
